/* eslint-disable no-self-assign */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable prefer-const */
import { ref, nextTick } from "@vue/runtime-core"
import { defineStore } from 'pinia'
import { useRouter } from "vue-router"
import Toast from "vant/lib/toast"
import 'vant/es/toast/style'

export const useStoriesStore = defineStore('stories', () => {
  interface CarType {
    img: string
    chima: string
    jiage: string
    title: string
    seletedcolor: string
    shopName: string
    num: number
    finish: boolean
    itemId: string
    id: number
  }

  const obj = ref()
  const router = useRouter()
  // eslint-disable-next-line @typescript-eslint/ban-types
  const cartList = ref<CarType[]>([])
  const shopList = ref<CarType[]>([])

  const carArr = localStorage.cartList
  if (carArr) {
    cartList.value = JSON.parse(carArr)
  }

  const shopArr = localStorage.shopList
  if (shopArr) {
    shopList.value = JSON.parse(shopArr)
  }

  const newCar = ref<CarType[]>([])

  const newArr = localStorage.newCar
  if (newArr) {
    newCar.value = JSON.parse(newArr)
  }

  // if (JSON.parse((localStorage.getItem("cartList") ?? ''))) {
  //   cartList.value = JSON.parse((localStorage.getItem("cartList") ?? ''))
  // } else {
  //   cartList.value = ref<CarType[]>([])
  // }

  // if (JSON.parse((localStorage.getItem("shopList") ?? ''))) {
  //   shopList.value = JSON.parse((localStorage.getItem("shopList") ?? ''))
  // } else {
  //   shopList.value = ref<CarType[]>([])
  // }

  // const cartList = JSON.parse((localStorage.getItem("cartList") ?? '')) || ref([])

  // const shopList = JSON.parse((localStorage.getItem("shopList") ?? '')) || ref([])

  function addOrder(img: string, chima: string, jiage: string, title: string, seletedcolor: string, shopName: string, num: number) {
    obj.value = {
      img,
      chima,
      jiage,
      title,
      seletedcolor,
      shopName,
      num,
    }
    // console.log(obj.value);
  }

  function addShoppingCart(img: string, chima: string, jiage: string, title: string, seletedcolor: string, shopName: string, num: number, finish: boolean, itemId: any) {
    if (shopList.value.length == 0) {
      shopList.value = [{ id: 1, shopName, img, chima, jiage, title, seletedcolor, num, finish, itemId }]
      cartList.value = [{ img, num, chima, title, jiage, shopName, finish, itemId, seletedcolor, id: 1 }, ...cartList.value]

      localStorage.setItem("shopList", JSON.stringify(shopList.value))
      localStorage.setItem("cartList", JSON.stringify(cartList.value))
    } else {
      if (shopList.value?.filter((res: any) => res.shopName == shopName).length != 0) {
        cartList.value = [{ img, num, chima, finish, seletedcolor, jiage, title, shopName, itemId, id: shopList.value.filter((res: any) => res.shopName == shopName)[0].id }, ...cartList.value]
      } else {
        let idNumber = shopList.value.length ? Math.max(...shopList.value.map((i: CarType) => i.id)) + 1 : 1
        shopList.value = [...shopList.value, { id: idNumber, shopName, img, chima, jiage, title, seletedcolor, num, finish, itemId }]
        cartList.value = [{ img, num, seletedcolor, jiage, chima, title, itemId, shopName, finish, id: idNumber }, ...cartList.value]
      }

      localStorage.setItem("cartList", JSON.stringify(cartList.value))
      localStorage.setItem("shopList", JSON.stringify(shopList.value))
    }
  }

  function minusNum(id: number, num: number) {
    nextTick(() => {
      cartList.value = cartList.value?.filter((o: CarType) => o.id === id ? { ...o, num } : o)
      shopList.value = shopList.value?.filter((o: CarType) => o.id === id ? { ...o, num } : o)
    })

  }

  function addNums(id: number, num: number) {
    nextTick(() => {
      cartList.value = cartList.value?.filter((o: CarType) => o.id === id ? { ...o, num } : o)
      shopList.value = shopList.value?.filter((o: CarType) => o.id === id ? { ...o, num } : o)
    })

  }

  function deleteGoods() {
    nextTick(() => {
      cartList.value = cartList.value?.filter((o: CarType) => o.finish === false)
      localStorage.cartList = JSON.stringify(cartList.value)
      router.go(0)
    })
  }

  function goOrder() {
    nextTick(() => {
      cartList.value = cartList.value.filter((o: CarType) => o.finish === true)
      cartList.value = [...cartList.value.filter((o: CarType) => o.finish === true), ...cartList.value.filter((o: CarType) => o.finish === false)]
      localStorage.cartList = JSON.stringify(cartList.value)
      router.push({ path: '/payment' })
    })
    // localStorage.setItem("cartList", JSON.stringify(cartList.value))

  }

  function getJiesuan() {
    cartList.value = cartList.value.filter((o) => o.finish === true)
    newCar.value = [...cartList.value, ...newCar.value]
    localStorage.newCar = JSON.stringify(newCar.value)
    cartList.value = cartList.value.filter((o) => o.finish === false)
    localStorage.cartList = JSON.stringify(cartList.value)
    Toast('支付成功')
    router.push({ path: '/dingdan' })
  }
  return { obj, addOrder, addShoppingCart, cartList, shopList, minusNum, addNums, deleteGoods, goOrder, getJiesuan, newCar }
})